//
//  WJCodeTableViewCell.m
//  JerryIos
//
//  Created by jerrywang on 2018/2/4.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJCodeTableCell.h"
#import "Masonry.h"

@interface WJCodeTableCell()

@property (nonatomic, weak) UIImageView *iconView;   //图标
@property (nonatomic, weak) UILabel *nameView;       //名称
@property (nonatomic, weak) UIImageView *vipView;    //vip
@property (nonatomic, weak) UILabel *textView;       //文本
@property (nonatomic, weak) UIImageView *picView;    //图片

@end

@implementation WJCodeTableCell

//重写自定义单元格的setFrame方法
- (void) setFrame:(CGRect)frame {
    frame.size.height -= 1;
    //此时才真正给cell赋值
    [super setFrame:frame];
}

+(instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *cellId = @"WJCodeTableCell";
    WJCodeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[WJCodeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

//构造方法（在初使化对象的时候调用）
//一般在这个方法中添加需要显示的子控件
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        _iconView = iconView;
        
        //名称
        UILabel *nameView = [[UILabel alloc] init];
        nameView.font = WJNameFont;
        [self.contentView addSubview:nameView];
        _nameView = nameView;
        
        //vip
        UIImageView *vipView = [[UIImageView alloc] init];
        vipView.image = [UIImage imageNamed:@"icon_vip"];
        [self.contentView addSubview:vipView];
        _vipView = vipView;
        
        //文本
        UILabel *textView = [[UILabel alloc] init];
        textView.numberOfLines = 0;
        textView.font = WJTextFont;
        [self.contentView addSubview:textView];
        _textView = textView;
        
        //图像
        UIImageView *picView = [[UIImageView alloc] init];
        [self.contentView addSubview:picView];
        _picView = picView;
    }
    return self;
}

//设置控件的frame和显示的数据
-(void) setPerson:(WJPerson *)person {
    _person = person;
    
    //设置显示的数据
    [self settingData];
    
    //第一种方式：计算frame方式
    [self layoutByFrame];
    //第二种方式：使用Masonry方式
    //[self layoutByMasonry];
}

//设置显示的数据
-(void) settingData {
    //图标
    _iconView.image = [UIImage imageNamed:self.person.icon];
    //名称
    _nameView.text = self.person.name;
    //vip
    if (self.person.vip) {
        _vipView.hidden = NO;
        _nameView.textColor = [UIColor redColor];
    } else {
        _vipView.hidden = YES;
        _nameView.textColor = [UIColor blueColor];
    }
    //文字
    _textView.text = self.person.text;
    //图像
    if (self.person.pic) {
        _picView.hidden = NO;
        _picView.image = [UIImage imageNamed:self.person.pic];
    } else {
        _picView.hidden = YES;
    }
}

//设置控件的frame
-(void) layoutByFrame {
    _iconView.frame = self.person.iconFrame;
    _nameView.frame = self.person.nameFrame;
    _vipView.frame = self.person.vipFrame;
    _textView.frame = self.person.textFrame;
    if (_person.pic) {
        _picView.frame = self.person.picFrame;
    }
}

-(void) layoutByMasonry {
    
}


@end
